/**
 * 
 */
package com.moorecat.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.moorecat.MooerApplication;
import com.moorecat.R;
import com.moorecat.bean.User;
import com.moorecat.http.HttpUtils;

/**
 * 更多 -> 意见反馈
 * 
 * @author Minglong.Yu
 * @version 1.0
 * @created Jun 22, 2011 12:06:40 PM
 */
public class MoreFeedback extends Activity implements OnClickListener {
	private ImageView sendBtn;
	private EditText feedback;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.more_feedback);

		feedback = (EditText) findViewById(R.id.feedback_text);
		sendBtn = (ImageView) findViewById(R.id.send_btn);
		sendBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		sendBtn.setClickable(false);
		String text = feedback.getText().toString();
		if (text != null && !"".equals(text.trim())) {
			if (progressDialog != null) {
				progressDialog.cancel();
			}
			progressDialog = new ProgressDialog(MoreFeedback.this);
			progressDialog
					.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage(MoreFeedback.this
					.getString(R.string.sending));
			progressDialog.setCancelable(false);
			progressDialog.show();
			new FeedbackTask().execute(text);
		} else {
			Toast t = Toast.makeText(getApplicationContext(), getResources()
					.getText(R.string.feedback_not_emtpy), Toast.LENGTH_LONG);
			t.show();
			sendBtn.setClickable(true);
		}
	}

	public class FeedbackTask extends AsyncTask<String, Integer, String> {
		private boolean success = false;

		@Override
		protected String doInBackground(String... params) {
			if (params != null && params.length > 0) {
				long userId = 0;
				try {
					MooerApplication app = (MooerApplication) MoreFeedback.this
					.getApplication();
					User user = app.getUser();
					if (app.isLoggedin() && user != null && user.getId() > 0) {
						userId = user.getId();
					}
					HttpUtils.doSubmitFeedback(userId, "", params[0]);
					success = true;
				} catch (Exception e) {
					success = false;
					e.printStackTrace();
				}
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			int msgId = 0;
			if (success) {
				msgId = R.string.feedback_success;
			} else {
				msgId = R.string.submit_exception_tip;
			}
			Dialog dialog = new AlertDialog.Builder(MoreFeedback.this)
					.setTitle(R.string.feedback)
					.setMessage(msgId)
					.setNegativeButton(R.string.ok,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									if (success)
										feedback.setText("");
									sendBtn.setClickable(true);
								}
							})
					.setOnCancelListener(
							new DialogInterface.OnCancelListener() {

								@Override
								public void onCancel(DialogInterface dialog) {
									sendBtn.setClickable(true);
								}

							}).create();
			dialog.show();
			progressDialog.cancel();
		}

	}
}
